﻿using System;
using System.Windows;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;
using System.Diagnostics;
using System.Windows.Browser;

namespace Ops.Mpc.Parameters
{
    /// <summary>
    /// This class returns Application.Current.Host.Source query string parameters as a dictionary
    /// </summary>
    public static class InitParameters
    {
        private static IDictionary<String, String> _initParams = null;
        private static object _lock = new object();

        public static IDictionary<string, string> GetCurrent()
        {
            lock (_lock)
            {
                if (_initParams == null)
                {
                    _initParams = new Dictionary<string, string>();

                    if (!string.IsNullOrWhiteSpace(Application.Current.Host.Source.Query))
                    {
                        string query = HttpUtility.UrlDecode(Application.Current.Host.Source.Query.TrimStart('?', ' '));
                        string[] nameValuePairs = query.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);

                        foreach (string nameValuePair in nameValuePairs)
                        {
                            string[] pieces = nameValuePair.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                            if (pieces.Length == 2)
                                _initParams.Add(pieces[0], pieces[1]);
                        }
                    }
                }
            }

            return _initParams;
        }




    }
}
